home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / Interfaces / CIncludes / MixedMode.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-23  |  14.1 KB  |  427 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        MixedMode.h
  3.  
  4.      Contains:    Mixed Mode Manager Interfaces.
  5.  
  6.      Version:    Technology:    System 7.5
  7.                  Package:    Universal Interfaces 2.1.2 on ETO #20
  8.  
  9.      Copyright:    © 1984-1995 by Apple Computer, Inc.
  10.                  All rights reserved.
  11.  
  12.      Bugs?:        If you find a problem with this file, use the Apple Bug Reporter
  13.                  stack.  Include the file and version information (from above)
  14.                  in the problem description and send to:
  15.                      Internet:    apple.bugs@applelink.apple.com
  16.                      AppleLink:    APPLE.BUGS
  17.  
  18. */
  19.  
  20. #ifndef __MIXEDMODE__
  21. #define __MIXEDMODE__
  22.  
  23.  
  24. #ifndef __TYPES__
  25. #include <Types.h>
  26. #endif
  27. /*    #include <ConditionalMacros.h>                                */
  28.  
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32.  
  33. #if PRAGMA_ALIGN_SUPPORTED
  34. #pragma options align=mac68k
  35. #endif
  36.  
  37. #if PRAGMA_IMPORT_SUPPORTED
  38. #pragma import on
  39. #endif
  40.  
  41.  
  42. enum {
  43.     kRoutineDescriptorVersion    = 7
  44. };
  45.  
  46. /* MixedModeMagic Magic Cookie/Trap number */
  47. enum {
  48.     _MixedModeMagic                = 0xAAFE
  49. };
  50.  
  51. /* Calling Conventions */
  52. typedef unsigned short CallingConventionType;
  53.  
  54.  
  55. enum {
  56.     kPascalStackBased            = (CallingConventionType)0,
  57.     kCStackBased                = (CallingConventionType)1,
  58.     kRegisterBased                = (CallingConventionType)2,
  59.     kD0DispatchedPascalStackBased = (CallingConventionType)8,
  60.     kD1DispatchedPascalStackBased = (CallingConventionType)12,
  61.     kD0DispatchedCStackBased    = (CallingConventionType)9,
  62.     kStackDispatchedPascalStackBased = (CallingConventionType)14,
  63.     kThinkCStackBased            = (CallingConventionType)5
  64. };
  65.  
  66. /* ISA Types */
  67. typedef SInt8 ISAType;
  68.  
  69.  
  70. enum {
  71.     kM68kISA                    = (ISAType)0,
  72.     kPowerPCISA                    = (ISAType)1
  73. };
  74.  
  75. /* RTA Types */
  76. typedef SInt8 RTAType;
  77.  
  78.  
  79. enum {
  80.     kOld68kRTA                    = (RTAType)(0 << 4),
  81.     kPowerPCRTA                    = (RTAType)(0 << 4),
  82.     kCFM68kRTA                    = (RTAType)(1 << 4)
  83. };
  84.  
  85. #if GENERATINGPOWERPC
  86. #define GetCurrentISA() ((ISAType) kPowerPCISA)
  87. #define GetCurrentRTA() ((RTAType) kPowerPCRTA)
  88. #else
  89. #define GetCurrentISA() ((ISAType) kM68kISA)
  90. #if GENERATINGCFM
  91. #define GetCurrentRTA() ((RTAType) kCFM68kRTA)
  92. #else
  93. #define GetCurrentRTA() ((RTAType) kOld68kRTA)
  94. #endif
  95. #endif
  96. #define GetCurrentArchitecture() (GetCurrentISA() | GetCurrentRTA())
  97. enum {
  98.     kRegisterD0                    = 0,
  99.     kRegisterD1                    = 1,
  100.     kRegisterD2                    = 2,
  101.     kRegisterD3                    = 3,
  102.     kRegisterD4                    = 8,
  103.     kRegisterD5                    = 9,
  104.     kRegisterD6                    = 10,
  105.     kRegisterD7                    = 11,
  106.     kRegisterA0                    = 4,
  107.     kRegisterA1                    = 5,
  108.     kRegisterA2                    = 6,
  109.     kRegisterA3                    = 7,
  110.     kRegisterA4                    = 12,
  111.     kRegisterA5                    = 13,
  112.     kRegisterA6                    = 14,
  113. /* A7 is the same as the PowerPC SP */
  114.     kCCRegisterCBit                = 16,
  115.     kCCRegisterVBit                = 17,
  116.     kCCRegisterZBit                = 18,
  117.     kCCRegisterNBit                = 19,
  118.     kCCRegisterXBit                = 20
  119. };
  120.  
  121. typedef unsigned short registerSelectorType;
  122.  
  123. /* SizeCodes we use everywhere */
  124.  
  125. enum {
  126.     kNoByteCode                    = 0,
  127.     kOneByteCode                = 1,
  128.     kTwoByteCode                = 2,
  129.     kFourByteCode                = 3
  130. };
  131.  
  132. /* Mixed Mode Routine Records */
  133. typedef unsigned long ProcInfoType;
  134.  
  135. /* Routine Flag Bits */
  136. typedef unsigned short RoutineFlagsType;
  137.  
  138.  
  139. enum {
  140.     kProcDescriptorIsAbsolute    = (RoutineFlagsType)0x00,
  141.     kProcDescriptorIsRelative    = (RoutineFlagsType)0x01
  142. };
  143.  
  144. enum {
  145.     kFragmentIsPrepared            = (RoutineFlagsType)0x00,
  146.     kFragmentNeedsPreparing        = (RoutineFlagsType)0x02
  147. };
  148.  
  149. enum {
  150.     kUseCurrentISA                = (RoutineFlagsType)0x00,
  151.     kUseNativeISA                = (RoutineFlagsType)0x04
  152. };
  153.  
  154. enum {
  155.     kPassSelector                = (RoutineFlagsType)0x0,
  156.     kDontPassSelector            = (RoutineFlagsType)0x08
  157. };
  158.  
  159. enum {
  160.     kRoutineIsNotDispatchedDefaultRoutine = (RoutineFlagsType)0x0,
  161.     kRoutineIsDispatchedDefaultRoutine = (RoutineFlagsType)0x10
  162. };
  163.  
  164. struct RoutineRecord {
  165.     ProcInfoType                    procInfo;                    /* calling conventions */
  166.     UInt8                            reserved1;                    /* Must be 0 */
  167.     ISAType                            ISA;                        /* Instruction Set Architecture */
  168.     RoutineFlagsType                routineFlags;                /* Flags for each routine */
  169.     ProcPtr                            procDescriptor;                /* Where is the thing we’re calling? */
  170.     UInt32                            reserved2;                    /* Must be 0 */
  171.     UInt32                            selector;                    /* For dispatched routines, the selector */
  172. };
  173. typedef struct RoutineRecord RoutineRecord;
  174.  
  175. typedef RoutineRecord *RoutineRecordPtr, **RoutineRecordHandle;
  176.  
  177. /* Mixed Mode Routine Descriptors */
  178. /* Definitions of the Routine Descriptor Flag Bits */
  179. typedef UInt8 RDFlagsType;
  180.  
  181.  
  182. enum {
  183.     kSelectorsAreNotIndexable    = (RDFlagsType)0x00,
  184.     kSelectorsAreIndexable        = (RDFlagsType)0x01
  185. };
  186.  
  187. /* Routine Descriptor Structure */
  188. struct RoutineDescriptor {
  189.     UInt16                            goMixedModeTrap;            /* Our A-Trap */
  190.     SInt8                            version;                    /* Current Routine Descriptor version */
  191.     RDFlagsType                        routineDescriptorFlags;        /* Routine Descriptor Flags */
  192.     UInt32                            reserved1;                    /* Unused, must be zero */
  193.     UInt8                            reserved2;                    /* Unused, must be zero */
  194.     UInt8                            selectorInfo;                /* If a dispatched routine, calling convention, else 0 */
  195.     UInt16                            routineCount;                /* Number of routines in this RD */
  196.     RoutineRecord                    routineRecords[1];            /* The individual routines */
  197. };
  198. typedef struct RoutineDescriptor RoutineDescriptor;
  199.  
  200. typedef RoutineDescriptor *RoutineDescriptorPtr, **RoutineDescriptorHandle;
  201.  
  202. #define BUILD_ROUTINE_DESCRIPTOR(procInfo, procedure)  \
  203.     {                                \
  204.     _MixedModeMagic,                \
  205.     kRoutineDescriptorVersion,        \
  206.     kSelectorsAreNotIndexable,        \
  207.     0,                                \
  208.     0,                                \
  209.     0,                                \
  210.     0,                                \
  211.     {                                \
  212.     {                                \
  213.     (procInfo),                    \
  214.     0,                                \
  215.     GetCurrentArchitecture(),     \
  216.     kProcDescriptorIsAbsolute |     \
  217.     kFragmentIsPrepared |            \
  218.     kUseNativeISA,                    \
  219.     (ProcPtr)(procedure),            \
  220.     0,                                \
  221.     0                                \
  222.     }                                \
  223.     }                                \
  224.     }
  225. #define BUILD_FAT_ROUTINE_DESCRIPTOR(m68kProcInfo, m68kProcPtr, powerPCProcInfo, powerPCProcPtr)  \
  226.     {                                \
  227.     _MixedModeMagic,                \
  228.     kRoutineDescriptorVersion,        \
  229.     kSelectorsAreNotIndexable,        \
  230.     0,                                \
  231.     0,                                \
  232.     0,                                \
  233.     1,                                \
  234.     {                                \
  235.     {                                \
  236.     (m68kProcInfo),                \
  237.     0,                                \
  238.     kM68kISA |                        \
  239.     kOld68kRTA,                    \
  240.     kProcDescriptorIsAbsolute |     \
  241.     kUseCurrentISA,                \
  242.     (ProcPtr)(m68kProcPtr),        \
  243.     0,                                \
  244.     0,                                \
  245.     },                                \
  246.     {                                \
  247.     (powerPCProcInfo),                \
  248.     0,                                \
  249.     GetCurrentArchitecture(),     \
  250.     kProcDescriptorIsAbsolute |     \
  251.     kFragmentIsPrepared |            \
  252.     kUseCurrentISA,                \
  253.     (ProcPtr)(powerPCProcPtr),        \
  254.     0,                                \
  255.     0                                \
  256.     }                                \
  257.     }                                \
  258.     }
  259.  
  260. enum {
  261. /* Calling Convention Offsets */
  262.     kCallingConventionWidth        = 4,
  263.     kCallingConventionPhase        = 0,
  264.     kCallingConventionMask        = 0xF,
  265. /* Result Offsets */
  266.     kResultSizeWidth            = 2,
  267.     kResultSizePhase            = kCallingConventionWidth,
  268.     kResultSizeMask                = 0x30,
  269. /* Parameter offsets & widths */
  270.     kStackParameterWidth        = 2,
  271.     kStackParameterPhase        = (kCallingConventionWidth + kResultSizeWidth),
  272.     kStackParameterMask            = 0xFFFFFFC0,
  273. /* Register Result Location offsets & widths */
  274.     kRegisterResultLocationWidth = 5,
  275.     kRegisterResultLocationPhase = (kCallingConventionWidth + kResultSizeWidth),
  276. /* Register Parameter offsets & widths */
  277.     kRegisterParameterWidth        = 5,
  278.     kRegisterParameterPhase        = (kCallingConventionWidth + kResultSizeWidth + kRegisterResultLocationWidth),
  279.     kRegisterParameterMask        = 0x7FFFF800,
  280.     kRegisterParameterSizePhase    = 0,
  281.     kRegisterParameterSizeWidth    = 2,
  282.     kRegisterParameterWhichPhase = kRegisterParameterSizeWidth,
  283.     kRegisterParameterWhichWidth = 3,
  284. /* Dispatched Stack Routine Selector offsets & widths */
  285.     kDispatchedSelectorSizeWidth = 2,
  286.     kDispatchedSelectorSizePhase = (kCallingConventionWidth + kResultSizeWidth),
  287. /* Dispatched Stack Routine Parameter offsets */
  288.     kDispatchedParameterPhase    = (kCallingConventionWidth + kResultSizeWidth + kDispatchedSelectorSizeWidth),
  289. /* Special Case offsets & widths */
  290.     kSpecialCaseSelectorWidth    = 6,
  291.     kSpecialCaseSelectorPhase    = kCallingConventionWidth,
  292.     kSpecialCaseSelectorMask    = 0x3F0,
  293. /* Component Manager Special Case offsets & widths */
  294.     kComponentMgrResultSizeWidth = 2,
  295.     kComponentMgrResultSizePhase = kCallingConventionWidth + kSpecialCaseSelectorWidth, /* 4 + 6 = 10 */
  296.     kComponentMgrParameterWidth    = 2,
  297.     kComponentMgrParameterPhase    = kComponentMgrResultSizePhase + kComponentMgrResultSizeWidth /* 10 + 2 = 12 */
  298. };
  299.  
  300. #define SIZE_CODE(size)         \
  301.     (((size) == 4) ? kFourByteCode : (((size) == 2) ? kTwoByteCode : (((size) == 1) ? kOneByteCode : 0)))
  302. #define RESULT_SIZE(sizeCode)     \
  303.     ((ProcInfoType)(sizeCode) << kResultSizePhase)
  304. #define STACK_ROUTINE_PARAMETER(whichParam, sizeCode)  \
  305.     ((ProcInfoType)(sizeCode) << (kStackParameterPhase + (((whichParam) - 1) * kStackParameterWidth)))
  306. #define DISPATCHED_STACK_ROUTINE_PARAMETER(whichParam, sizeCode)  \
  307.     ((ProcInfoType)(sizeCode) << (kDispatchedParameterPhase + (((whichParam) - 1) * kStackParameterWidth)))
  308. #define DISPATCHED_STACK_ROUTINE_SELECTOR_SIZE(sizeCode)  \
  309.     ((ProcInfoType)(sizeCode) << kDispatchedSelectorSizePhase)
  310. #define REGISTER_RESULT_LOCATION(whichReg)  \
  311.     ((ProcInfoType)(whichReg) << kRegisterResultLocationPhase)
  312. #define REGISTER_ROUTINE_PARAMETER(whichParam, whichReg, sizeCode)  \
  313.     ((((ProcInfoType)(sizeCode) << kRegisterParameterSizePhase) | ((ProcInfoType)(whichReg) << kRegisterParameterWhichPhase)) <<  \
  314.     (kRegisterParameterPhase + (((whichParam) - 1) * kRegisterParameterWidth)))
  315. #define COMPONENT_MGR_RESULT_SIZE(sizeCode)  \
  316.     ((ProcInfoType)(sizeCode) << kComponentMgrResultSizePhase)
  317. #define COMPONENT_MGR_PARAMETER(whichParam, sizeCode)  \
  318.     ((ProcInfoType)(sizeCode) << (kComponentMgrParameterPhase + (((whichParam) - 1) * kComponentMgrParameterWidth)))
  319. #define SPECIAL_CASE_PROCINFO(specialCaseCode)  \
  320.     (kSpecialCase | ((ProcInfoType)(specialCaseCode) << 4))
  321. enum {
  322.     kSpecialCase                = (CallingConventionType)0x0000000F
  323. };
  324.  
  325. enum {
  326. /* all of the special cases enumerated.  The selector field is 6 bits wide */
  327.     kSpecialCaseHighHook        = 0,
  328.     kSpecialCaseCaretHook        = 0,                            /* same as kSpecialCaseHighHook */
  329.     kSpecialCaseEOLHook            = 1,
  330.     kSpecialCaseWidthHook        = 2,
  331.     kSpecialCaseTextWidthHook    = 2,                            /* same as kSpecialCaseWidthHook */
  332.     kSpecialCaseNWidthHook        = 3,
  333.     kSpecialCaseDrawHook        = 4,
  334.     kSpecialCaseHitTestHook        = 5,
  335.     kSpecialCaseTEFindWord        = 6,
  336.     kSpecialCaseProtocolHandler    = 7,
  337.     kSpecialCaseSocketListener    = 8,
  338.     kSpecialCaseTERecalc        = 9,
  339.     kSpecialCaseTEDoText        = 10,
  340.     kSpecialCaseGNEFilterProc    = 11,
  341.     kSpecialCaseMBarHook        = 12,
  342.     kSpecialCaseComponentMgr    = 13
  343. };
  344.  
  345. /*
  346.     NOTES ON USING New[Fat]RoutineDescriptor[Trap] 
  347.     
  348.     When calling these routine from classic 68k code there are two possible intentions.
  349.  
  350.     The first is source compatibility with code ported to CFM (either PowerPC or 68k CFM). When
  351.     the code is compiled for CFM the functions create routine descriptors that can be used by
  352.     the mixed mode manager operating on that machine. When the code is compiled for classic 68k
  353.     these functions do nothing so that the code will run on Macintoshes that do not have a
  354.     mixed mode manager. The dual nature of these functions is achieved by turning the CFM calls
  355.     into "no-op" macros for classic 68k: You can put "NewRoutineDescriptor" in your source,
  356.     compile it for any runtime or instruction set architecture, and it will run correctly on the
  357.     intended runtime/instruction platform. All without source changes and/or conditional source.
  358.     
  359.     The other intention is for code that "knows" that it is executing as classic 68k runtime
  360.     and is specifically trying to call code of another architecture using mixed mode. Since the
  361.     routines were designed with classic <-> CFM source compatibility in mind this second case
  362.     is treated special. For classic 68k code to create routines descriptors for use by mixed mode
  363.     it must call the "Trap" versions of the routines (NewRoutineDescriptorTrap). These versions
  364.     are only available to classic 68k callers: rigging the interfaces to allow calling them
  365.     from CFM code will result in runtime failure because no shared library implements or exports
  366.     the functions.
  367.     
  368.  
  369.     This almost appears seamless until you consider "fat" routine descriptors and the advent of
  370.     CFM-68K. What does "fat" mean? CFM-68K is not emulated on PowerPC and PowerPC is not emulated
  371.     on CFM-68K. It makes no sense to create a routine descriptor having both a CFM-68K routine
  372.     and a PowerPC native routine pointer. Therefore "fat" is defined to be a mix of classic and
  373.     CFM for the hardware's native instruction set: on PowerPC fat is classic and PowerPC native,
  374.     on a 68k machine with CFM-68K installed fat is classic and CFM-68K.
  375.     
  376.     By definition fat routine descriptors are only constructed by code that is aware of the 
  377.     architecture it is executing as and that another architecture exists. Source compatibility
  378.     between code intented as pure classic and pure CFM is not an issue and so NewFatRoutineDescriptor
  379.     is not available when building pure classic code.
  380.     
  381.     NewFatRoutineDescriptorTrap is available to classic code on both PowerPC and CFM-68K. The
  382.     classic code can use the code fragment manager routine "FindSymbol" to obtain the address of 
  383.     a routine in a shared library and then construct a routine descriptor with both the CFM routine 
  384.     and classic    routine.
  385. */    
  386.  
  387. #if GENERATINGCFM
  388. extern pascal UniversalProcPtr NewRoutineDescriptor(ProcPtr theProc, ProcInfoType theProcInfo, ISAType theISA);
  389. extern pascal void DisposeRoutineDescriptor(UniversalProcPtr theProcPtr);
  390. extern pascal UniversalProcPtr NewFatRoutineDescriptor(ProcPtr theM68kProc, ProcPtr thePowerPCProc, ProcInfoType theProcInfo);
  391. #else
  392. #define DisposeRoutineDescriptor(theProcPtr)
  393. #define NewRoutineDescriptor(theProc, theProcInfo, theISA) ((UniversalProcPtr)theProc)
  394. /* Note that the call to NewFatRoutineDescriptor is undefined when GENERATINGCFM is false. */
  395.  
  396. extern pascal UniversalProcPtr NewRoutineDescriptorTrap(ProcPtr theProc, ProcInfoType theProcInfo, ISAType theISA)
  397.  TWOWORDINLINE(0x7000, 0xAA59);
  398. extern pascal void DisposeRoutineDescriptorTrap(UniversalProcPtr theProcPtr)
  399.  TWOWORDINLINE(0x7001, 0xAA59);
  400. extern pascal UniversalProcPtr NewFatRoutineDescriptorTrap(ProcPtr theM68kProc, ProcPtr thePowerPCProc, ProcInfoType theProcInfo)
  401.  TWOWORDINLINE(0x7002, 0xAA59);
  402. #endif
  403.  
  404. #if GENERATINGCFM
  405. /*
  406.  CallUniversalProc is only implemented in shared libraries on 68k and PowerPC, it is now
  407.  conditionalize with GENERATINGCFM.  This will catch accidental calls from classic 68K code
  408.  that previously only showed up as linker errors.
  409. */
  410. extern long CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
  411. extern long CallOSTrapUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
  412. #endif
  413.  
  414. #if PRAGMA_IMPORT_SUPPORTED
  415. #pragma import off
  416. #endif
  417.  
  418. #if PRAGMA_ALIGN_SUPPORTED
  419. #pragma options align=reset
  420. #endif
  421.  
  422. #ifdef __cplusplus
  423. }
  424. #endif
  425.  
  426. #endif /* __MIXEDMODE__ */
  427.